#include "TimX.h"

#define SYSTEM_TEST_1S_PRI

uint16_t TIM_cnt = 0;

void RccX_Config()
{
	RCC_ConfigPclk1(RCC_HCLK_DIV1);
}

void TimX_init(uint16_t prex)		//SystemCoreClock = 64000000,APB1 = 16000000
{
	TIM_TimeBaseInitType TIM_TimeBaseStructure;

	RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
	
	TIM_TimeBaseStructure.Period    = 10000;  
    TIM_TimeBaseStructure.Prescaler = (prex - 1);
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
	TIM_ClearFlag(TIM2, TIM_FLAG_UPDATE);
    TIM_ConfigInt(TIM2, TIM_INT_UPDATE, ENABLE);

    TIM_Enable(TIM2, ENABLE);	
}

void TIM2_IRQHandler(void)
{
	if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) != RESET)
	{
#ifdef SYSTEM_TEST_1S_PRI
		TIM_cnt += 1;
		printf("Tim_cnt= %d\n",TIM_cnt);
		if(TIM_cnt == 0xFFFF)
			TIM_cnt = 0;
#endif
		TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);
	}
}
